package gui;

import javax.swing.JDialog;
import javax.swing.JFrame;

import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.text.SimpleDateFormat;

import javax.swing.JLabel;

import datos.ControlHoras;
import datos.Horas;

import gui.beans.MyCalendar;
import gui.beans.MyIntegerField;
import gui.eventos.ChangeDateEvent;
import gui.eventos.ChangeDateListener;
import gui.eventos.UpdateIntegerEvent;
import gui.eventos.UpdateIntegerListener;

@SuppressWarnings("serial")
public class InterfazHoras extends JDialog {

	private JPanel jContentPane = null;
	private JPanel jPCalendario = null;
	private JPanel jPHoras = null;
	private MyCalendar jCalendar = null;
	private JLabel jLAnalisis = null;
	private JLabel jLPruebas = null;
	private JLabel jLTotal = null;
	private MyIntegerField jTFAnalisis = null;
	private MyIntegerField jTFPruebas = null;
	private JLabel jLTotalCalculado = null;
	private JLabel jLTitulo = null;

	private ControlHoras ch = null;
	
	/**
	 * This method initializes 
	 * 
	 */
	public InterfazHoras(JFrame frame, ControlHoras ch) {
		super(frame);
		this.ch = ch;
		initialize(frame);
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize(JFrame frame) {
        this.setSize(new Dimension(500, 200));
        this.setTitle("Seguimiento de horas invertidas");
        this.setContentPane(getJContentPane());
        this.setLocationRelativeTo(frame);
        this.setModal(true);
		this.setVisible(true);
		
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			gridBagConstraints1.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridheight = 2;
			gridBagConstraints.gridy = 0;
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(getJPCalendario(), gridBagConstraints);
			jContentPane.add(getJPHoras(), gridBagConstraints1);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPCalendario	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPCalendario() {
		if (jPCalendario == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.weighty = 1.0;
			gridBagConstraints2.gridy = 0;
			jPCalendario = new JPanel();
			jPCalendario.setLayout(new GridBagLayout());
			jPCalendario.add(getJCalendar(), gridBagConstraints2);
		}
		return jPCalendario;
	}

	/**
	 * This method initializes jPHoras	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPHoras() {
		if (jPHoras == null) {
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 0;
			gridBagConstraints9.gridwidth = 2;
			gridBagConstraints9.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints9.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints9.gridy = 0;
			jLTitulo = new JLabel();
			jLTitulo.setText("Horas invertidas en la incidencia:");
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 1;
			gridBagConstraints8.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints8.weightx = 1.0;
			gridBagConstraints8.gridy = 3;
			jLTotalCalculado = new JLabel();
			jLTotalCalculado.setText("");
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.BOTH;
			gridBagConstraints7.gridy = 2;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints7.gridx = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.BOTH;
			gridBagConstraints6.gridy = 1;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints6.gridx = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridy = 3;
			jLTotal = new JLabel();
			jLTotal.setText("Total:");
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.gridy = 2;
			jLPruebas = new JLabel();
			jLPruebas.setText("Pruebas:");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.gridy = 1;
			jLAnalisis = new JLabel();
			jLAnalisis.setText("Analsis:");
			jPHoras = new JPanel();
			jPHoras.setLayout(new GridBagLayout());
			jPHoras.add(jLAnalisis, gridBagConstraints3);
			jPHoras.add(jLPruebas, gridBagConstraints4);
			jPHoras.add(jLTotal, gridBagConstraints5);
			jPHoras.add(getJTFAnalisis(), gridBagConstraints6);
			jPHoras.add(getJTFPruebas(), gridBagConstraints7);
			jPHoras.add(jLTotalCalculado, gridBagConstraints8);
			jPHoras.add(jLTitulo, gridBagConstraints9);
		}
		return jPHoras;
	}
	
	void cargarHoras(String date){
		Horas h = ch.obtenerHoras(date);
		if(h==null)
			h=new Horas();
		getJTFAnalisis().setValue(new Integer(h.analisis()));
		getJTFPruebas().setValue(new Integer(h.pruebas()));
		actualizaHorasTotales(date);
	}
	
	private void actualizaHorasTotales(String fecha){
		jLTotalCalculado.setText(Integer.toString(ch.obtenerHorasTotales(fecha)));
	}
	
	/**
	 * This method initializes jCalendar	
	 */
	private MyCalendar getJCalendar() {
		if (jCalendar == null) {
			jCalendar = new MyCalendar();
			jCalendar.addChangeDateListener(new ChangeDateListener(){
				public void dateChanged(ChangeDateEvent e){
					cargarHoras(e.getValue());
					SimpleDateFormat df = new SimpleDateFormat("EEEE d 'de' MMMM 'de' yyyy");
					jLTitulo.setText("Horas invertidas el " + df.format(e.getDate()));
				}
			});
		}
		return jCalendar;
	}

	/**
	 * This method initializes jTFAnalisis	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private MyIntegerField getJTFAnalisis() {
		if (jTFAnalisis == null) {
			jTFAnalisis = new MyIntegerField();
			jTFAnalisis.addUpdateIntegerListener(new UpdateIntegerListener(){
				public void valueUpdated(UpdateIntegerEvent e) {
					ch.fijaHorasAnalisis(jCalendar.getStringDate(), e.getValue());
					actualizaHorasTotales(jCalendar.getStringDate());
				}				
			});
		}
		return jTFAnalisis;
	}

	/**
	 * This method initializes jTFPruebas	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private MyIntegerField getJTFPruebas() {
		if (jTFPruebas == null) {
			jTFPruebas = new MyIntegerField();
			jTFPruebas.addUpdateIntegerListener(new UpdateIntegerListener(){
				public void valueUpdated(UpdateIntegerEvent e) {
					ch.fijaHorasPruebas(jCalendar.getStringDate(), e.getValue());
					actualizaHorasTotales(jCalendar.getStringDate());
				}				
			});
		}
		return jTFPruebas;
	}

}  //  @jve:decl-index=0:visual-constraint="193,136"
